#include	"main.h"

#define LOOPS 0

/********************************************************
**函数名：	BSW_LPUART_INIT
**功能：		初始化 LPSPI_INIT
**输入参数	Lpsi_Cfg	SPI配置结构体
**返回值：	无
**注意：		根据相应的电路配置SPI
*********************************************************/
void BSW_LPUART_INIT(Bsw_Lpuart_Cfg Lpuart_Cfg)
{
	PCC_SetClockMode(PCC, Lpuart_Cfg.clockName, false);                                                    /* 为给定的外设启用/禁用时钟 eg:禁止                                    	  */ 
	PCC_SetPeripheralClockControl(PCC, Lpuart_Cfg.clockName, true, Lpuart_Cfg.clockSource, 0, 0);            /* 设置SOSC控制寄存器                                                    	*/
	PCC_SetClockMode(PCC,Lpuart_Cfg.clockName, true);                                                      /* 为给定的外设启用/禁用时钟 eg:启用                                     	*/ 
	
	LPUART_Init(Lpuart_Cfg.base);
	
	LPUART_SetBaudRateDivisor(Lpuart_Cfg.base,Lpuart_Cfg.BaudRate);														/*	SBR=52 baud divisor = 40M/115200/16 = 21.7*/
	LPUART_SetOversamplingRatio(Lpuart_Cfg.base,Lpuart_Cfg.OSRatio);													/*	OSR=15: Over sampling ratio = 15+1=16 */
	LPUART_SetStopBitCount(Lpuart_Cfg.base,Lpuart_Cfg.StopBit);							/*	SBNS=0: One stop bit */
	LPUART_SetBothEdgeSamplingCmd(Lpuart_Cfg.base,Lpuart_Cfg.BothEgSCmd);											/*	BOTHEDGE=0: receiver samples only on rising edge */
	LPUART_SetBitCountPerChar(Lpuart_Cfg.base,Lpuart_Cfg.DataBit);				/*	M10=0: Rx and Tx use 7 to 9 bit data characters */

	LPUART_SetTxDmaCmd(Lpuart_Cfg.base,Lpuart_Cfg.TxDmaCmd);	      													/*	TDMAE, TDMAE=0: DMA requests disabled */
	LPUART_SetRxDmaCmd(Lpuart_Cfg.base,Lpuart_Cfg.RxDmaCmd);																	/*	RDMAE, RDMAE=0: DMA requests disabled */
	/* MAEN1, MAEN2,  MATCFG=0: Match disabled */
	

	
  /* RESYNCDIS=0: Resync during rec'd data word supported */
	LPUART_SetIntMode(Lpuart_Cfg.base,LPUART_INT_RX_ACTIVE_EDGE,false); 			/*	RXEDGIE=0: interrupts disable */
	LPUART_SetIntMode(Lpuart_Cfg.base,LPUART_INT_TX_COMPLETE,false);					/*	LBKDIE=0:  interrupts disable*/
	
	LPUART_SetIntMode(Lpuart_Cfg.base,LPUART_INT_LIN_BREAK_DETECT,false); 		/*	RXEDGIE=0: interrupts disable */
	LPUART_SetIntMode(Lpuart_Cfg.base,LPUART_INT_RX_DATA_REG_FULL,false);			/*	LBKDIE=0:  interrupts disable*/
	
	
	
	
	LPUART_SetTransmitterCmd(Lpuart_Cfg.base,Lpuart_Cfg.TxCmd);														/* TE=1: Transmitter enabled */
	LPUART_SetReceiverCmd(Lpuart_Cfg.base,Lpuart_Cfg.RxCmd);															/* RE=1: Receiver enabled */	
	LPUART_SetParityMode(Lpuart_Cfg.base,Lpuart_Cfg.PrtMode);									/* PE,PT=0: No hw parity generation or checking */
	/* M7,M,R8T9,R9T8=0: 8-bit data characters*/
  /* DOZEEN=0: LPUART enabled in Doze mode */
//	LPUART_SetIntMode();         /* ORIE,NEIE,FEIE,PEIE,TIE,TCIE,RIE,ILIE,MA1IE,MA2IE=0: no IRQ*/
	/* TxDIR=0: TxD pin is input if in single-wire mode */
	/* TXINV=0: TRansmit data not inverted */
	/* RWU,WAKE=0: normal operation; rcvr not in statndby */
	/* IDLCFG=0: one idle character */
	/* ILT=0: Idle char bit count starts after start bit */
	/* SBK=0: Normal transmitter operation - no break char */
	/* LOOPS,RSRC=0: no loop back */
#if LOOPS == 1
	//////////////loopback/////////////////
    Lpuart_Cfg.base->CTRL |= LPUART_CTRL_LOOPS_MASK;
    Lpuart_Cfg.base->CTRL &= ~LPUART_CTRL_RSRC_MASK;
#endif
	Lpuart_Cfg.base -> WATER &= ~LPUART_WATER_TXWATER_MASK; 
	Lpuart_Cfg.base -> WATER |= LPUART_WATER_TXWATER(Lpuart_Cfg.TxWater)<<LPUART_WATER_TXWATER_SHIFT; 
	
	Lpuart_Cfg.base -> WATER &= ~LPUART_WATER_RXWATER_MASK; 
	Lpuart_Cfg.base -> WATER |= LPUART_WATER_RXWATER(Lpuart_Cfg.RxWater)<<LPUART_WATER_RXWATER_SHIFT; 
	
//	LPUART_SetBreakCharTransmitLength(Lpuart_Cfg.base,LPUART_BREAK_CHAR_10_BIT_MINIMUM);
//	LPUART_SetBreakCharDetectLength(Lpuart_Cfg.base,LPUART_BREAK_CHAR_10_BIT_MINIMUM);
//	LPUART_SetRxDataPolarity(Lpuart_Cfg.base,false);
}




void BSW_LPUART_SData(LPUART_Type* base,u8 send) 
{ 
	while((base->STAT & LPUART_STAT_TDRE_MASK)>>LPUART_STAT_TDRE_SHIFT==0);                                 /* Wait for transmit buffer to be empty */
	base->DATA = send; 
}


void BSW_LPUART_Send_Char(LPUART_Type* base,u8* DataArr) 
{ 
	u8 u8_Count = 0;
	while(DataArr[u8_Count] != '\n')
	{
		BSW_LPUART_SData(base,DataArr[u8_Count]);
		u8_Count ++;
	}
//	BSW_LPUART_SData(base,'\n');
//	BSW_LPUART_SData(base,0x0D);
//	BSW_LPUART_SData(base,0x0A);
}



/* Function to Receive single Char */
u8 BSW_LPUART_Receive_char(LPUART_Type* base)
{    
  u8 recieve;
//	LPUART_GetStatusFlag(base,LPUART_RX_DATA_REG_FULL);
  while((base->STAT & LPUART_STAT_RDRF_MASK)>>LPUART_STAT_RDRF_SHIFT==0); 				/* Wait for received buffer to be full */
  recieve = base->DATA;            /* Read received data*/
  return recieve;
}
















